################################
# 11_prepareRollCallMexico-4.R #
################################

rm(list=ls())
print (version)

# platform       aarch64-apple-darwin20      
# arch           aarch64                     
# os             darwin20                    
# system         aarch64, darwin20           
# status                                     
# major          4                           
# minor          1.2                         
# year           2021                        
# month          11                          
# day            01                          
# svn rev        81115                       
# language       R                           
# version.string R version 4.1.2 (2021-11-01)
# nickname       Bird Hippie          

library (runjags)
library (coda)
library (reshape2)
library (random)

# Set working directory
savePathRep <- "/all_data/"
setwd (savePathRep)

set.seed (10002)
load ("mexico_rollcall_intermediate_mean.RData")
  
RC.2 <- as.data.frame (full.roll.call)   

leg.index  <- 1:nrow(RC.2)
vote.index <- 1:ncol(RC.2)
colnames (RC.2) <- vote.index
RC.2$leg <- leg.index
molten.RC <- reshape2::melt(RC.2, id.vars="leg", variable.name="vote", value.name="RC")

# Vectorize party information
comp.party <- as.data.frame (full.party.membership)

colnames (comp.party) <- vote.index
comp.party$leg  <- leg.index
molten.comp.party <- reshape2::melt(comp.party, id.vars="leg", variable.name="vote", value.name="party")
molten.comp.party$original.order <- 1:nrow(molten.comp.party)

# Here, there are a couple of legislators with missing parties.
# Salazar Saenz Francisco Javier (#354) is 1
# Gonzalez Ilescas Jorge Venustiano (#239) is 2
# But they were only in the legislature for a handful of votes, which we have identified as having the following row.names in molten.comp.party:
# Salazar Saenz: 19778
# Gonzalez Ilescas:59118, 59725, 60332, 60939, 61546, 62153, 62760, 63367, 63974
molten.comp.party$party[19778] <- 1
molten.comp.party$party[c(59118, 59725, 60332, 60939, 61546, 62153, 62760, 63367, 63974)] <- 2


# Find legislators that switched parties; these will be modeled as different people
molten.comp.party <- molten.comp.party[with(molten.comp.party, order(molten.comp.party$leg, molten.comp.party$party)), ]
molten.comp.party$party.noNA <- car::recode (molten.comp.party$party, "NA=99")
new.leg.no <- c()
start <- 0
for (i in 1:max(molten.comp.party$leg)){
  which.leg <- molten.comp.party[molten.comp.party$leg==i,]
  new.leg.all <- c()
  for (j in 1:length (rle (which.leg$party.noNA)$lengths)){
    new.leg <- rep (start+1, rle (which.leg$party.noNA)$lengths[j])
    start <- start+1
    new.leg.all <- c(new.leg.all, new.leg)
  }
  new.leg.no <- c(new.leg.no, new.leg.all)
}
molten.comp.party$new.leg.no <- new.leg.no
molten.comp.party <- molten.comp.party[with(molten.comp.party, order(molten.comp.party$original.order)),]

### Melt R matrices
# We need to melt all R# objects, which contain party.pressure.for * some dummy
R1 <- as.data.frame (R1)
R2 <- as.data.frame (R2)
R3 <- as.data.frame (R3)
R4 <- as.data.frame (R4)
R5 <- as.data.frame (R5)
R6 <- as.data.frame (R6)
R7 <- as.data.frame (R7)
R8 <- as.data.frame (R8)
R9 <- as.data.frame (R9)
R10 <- as.data.frame (R10)
R11 <- as.data.frame (R11)
R12 <- as.data.frame (R12)
R13 <- as.data.frame (R13)
R14 <- as.data.frame (R14)
R15 <- as.data.frame (R15)
R16 <- as.data.frame (R16)
R17 <- as.data.frame (R17)
R18 <- as.data.frame (R18)
R19 <- as.data.frame (R19)
R20 <- as.data.frame (R20)
R21 <- as.data.frame (R21)
R22 <- as.data.frame (R22)
R23 <- as.data.frame (R23)
R24 <- as.data.frame (R24)
R25 <- as.data.frame (R25)
R26 <- as.data.frame (R26)
R27 <- as.data.frame (R27)
R28 <- as.data.frame (R28)
R29 <- as.data.frame (R29)
R30 <- as.data.frame (R30)
R31 <- as.data.frame (R31)
R32 <- as.data.frame (R32)
R33 <- as.data.frame (R33)
R34 <- as.data.frame (R34)
R35 <- as.data.frame (R35)
R36 <- as.data.frame (R36)
R37 <- as.data.frame (R37)
R38 <- as.data.frame (R38)
R39 <- as.data.frame (R39)
R40 <- as.data.frame (R40)
R41 <- as.data.frame (R41)
R42 <- as.data.frame (R42)
R43 <- as.data.frame (R43)
R44 <- as.data.frame (R44)
R45 <- as.data.frame (R45)
R46 <- as.data.frame (R46)
R47 <- as.data.frame (R47)
R48 <- as.data.frame (R48)
R49 <- as.data.frame (R49)
R50 <- as.data.frame (R50)
R51 <- as.data.frame (R51)
R52 <- as.data.frame (R52)
R53 <- as.data.frame (R53)
R54 <- as.data.frame (R54)
R55 <- as.data.frame (R55)
R56 <- as.data.frame (R56)
R57 <- as.data.frame (R57)
R58 <- as.data.frame (R58)
R59 <- as.data.frame (R59)
R60 <- as.data.frame (R60)
R61 <- as.data.frame (R61)
R62 <- as.data.frame (R62)
R63 <- as.data.frame (R63)
R64 <- as.data.frame (R64)
R65 <- as.data.frame (R65)
R66 <- as.data.frame (R66)
R67 <- as.data.frame (R67)
R68 <- as.data.frame (R68)
R69 <- as.data.frame (R69)
R70 <- as.data.frame (R70)
R71 <- as.data.frame (R71)
R72 <- as.data.frame (R72)
R73 <- as.data.frame (R73)
R74 <- as.data.frame (R74)
R75 <- as.data.frame (R75)
R76 <- as.data.frame (R76)
R77 <- as.data.frame (R77)
R78 <- as.data.frame (R78)
R79 <- as.data.frame (R79)
R80 <- as.data.frame (R80)
R81 <- as.data.frame (R81)
R82 <- as.data.frame (R82)
R83 <- as.data.frame (R83)
R84 <- as.data.frame (R84)
R85 <- as.data.frame (R85)
R86 <- as.data.frame (R86)
R87 <- as.data.frame (R87)
R88 <- as.data.frame (R88)

colnames (R1) <- vote.index
colnames (R2) <- vote.index
colnames (R3) <- vote.index
colnames (R4) <- vote.index
colnames (R5) <- vote.index
colnames (R6) <- vote.index
colnames (R7) <- vote.index
colnames (R8) <- vote.index
colnames (R9) <- vote.index
colnames (R10) <- vote.index
colnames (R11) <- vote.index
colnames (R12) <- vote.index
colnames (R13) <- vote.index
colnames (R14) <- vote.index
colnames (R15) <- vote.index
colnames (R16) <- vote.index
colnames (R17) <- vote.index
colnames (R18) <- vote.index
colnames (R19) <- vote.index
colnames (R20) <- vote.index
colnames (R21) <- vote.index
colnames (R22) <- vote.index
colnames (R23) <- vote.index
colnames (R24) <- vote.index
colnames (R25) <- vote.index
colnames (R26) <- vote.index
colnames (R27) <- vote.index
colnames (R28) <- vote.index
colnames (R29) <- vote.index
colnames (R30) <- vote.index
colnames (R31) <- vote.index
colnames (R32) <- vote.index
colnames (R33) <- vote.index
colnames (R34) <- vote.index
colnames (R35) <- vote.index
colnames (R36) <- vote.index
colnames (R37) <- vote.index
colnames (R38) <- vote.index
colnames (R39) <- vote.index
colnames (R40) <- vote.index
colnames (R41) <- vote.index
colnames (R42) <- vote.index
colnames (R43) <- vote.index
colnames (R44) <- vote.index
colnames (R45) <- vote.index
colnames (R46) <- vote.index
colnames (R47) <- vote.index
colnames (R48) <- vote.index
colnames (R49) <- vote.index
colnames (R50) <- vote.index
colnames (R51) <- vote.index
colnames (R52) <- vote.index
colnames (R53) <- vote.index
colnames (R54) <- vote.index
colnames (R55) <- vote.index
colnames (R56) <- vote.index
colnames (R57) <- vote.index
colnames (R58) <- vote.index
colnames (R59) <- vote.index
colnames (R60) <- vote.index
colnames (R61) <- vote.index
colnames (R62) <- vote.index
colnames (R63) <- vote.index
colnames (R64) <- vote.index
colnames (R65) <- vote.index
colnames (R66) <- vote.index
colnames (R67) <- vote.index
colnames (R68) <- vote.index
colnames (R69) <- vote.index
colnames (R70) <- vote.index
colnames (R71) <- vote.index
colnames (R72) <- vote.index
colnames (R73) <- vote.index
colnames (R74) <- vote.index
colnames (R75) <- vote.index
colnames (R76) <- vote.index
colnames (R77) <- vote.index
colnames (R78) <- vote.index
colnames (R79) <- vote.index
colnames (R80) <- vote.index
colnames (R81) <- vote.index
colnames (R82) <- vote.index
colnames (R83) <- vote.index
colnames (R84) <- vote.index
colnames (R85) <- vote.index
colnames (R86) <- vote.index
colnames (R87) <- vote.index
colnames (R88) <- vote.index


R1$leg <- leg.index
R2$leg <- leg.index
R3$leg <- leg.index
R4$leg <- leg.index
R5$leg <- leg.index
R6$leg <- leg.index
R7$leg <- leg.index
R8$leg <- leg.index
R9$leg <- leg.index
R10$leg <- leg.index
R11$leg <- leg.index
R12$leg <- leg.index
R13$leg <- leg.index
R14$leg <- leg.index
R15$leg <- leg.index
R16$leg <- leg.index
R17$leg <- leg.index
R18$leg <- leg.index
R19$leg <- leg.index
R20$leg <- leg.index
R21$leg <- leg.index
R22$leg <- leg.index
R23$leg <- leg.index
R24$leg <- leg.index
R25$leg <- leg.index
R26$leg <- leg.index
R27$leg <- leg.index
R28$leg <- leg.index
R29$leg <- leg.index
R30$leg <- leg.index
R31$leg <- leg.index
R32$leg <- leg.index
R33$leg <- leg.index
R34$leg <- leg.index
R35$leg <- leg.index
R36$leg <- leg.index
R37$leg <- leg.index
R38$leg <- leg.index
R39$leg <- leg.index
R40$leg <- leg.index
R41$leg <- leg.index
R42$leg <- leg.index
R43$leg <- leg.index
R44$leg <- leg.index
R45$leg <- leg.index
R46$leg <- leg.index
R47$leg <- leg.index
R48$leg <- leg.index
R49$leg <- leg.index
R50$leg <- leg.index
R51$leg <- leg.index
R52$leg <- leg.index
R53$leg <- leg.index
R54$leg <- leg.index
R55$leg <- leg.index
R56$leg <- leg.index
R57$leg <- leg.index
R58$leg <- leg.index
R59$leg <- leg.index
R60$leg <- leg.index
R61$leg <- leg.index
R62$leg <- leg.index
R63$leg <- leg.index
R64$leg <- leg.index
R65$leg <- leg.index
R66$leg <- leg.index
R67$leg <- leg.index
R68$leg <- leg.index
R69$leg <- leg.index
R70$leg <- leg.index
R71$leg <- leg.index
R72$leg <- leg.index
R73$leg <- leg.index
R74$leg <- leg.index
R75$leg <- leg.index
R76$leg <- leg.index
R77$leg <- leg.index
R78$leg <- leg.index
R79$leg <- leg.index
R80$leg <- leg.index
R81$leg <- leg.index
R82$leg <- leg.index
R83$leg <- leg.index
R84$leg <- leg.index
R85$leg <- leg.index
R86$leg <- leg.index
R87$leg <- leg.index
R88$leg <- leg.index

# These vectors will be longer, because they include three additional legislators whose rows were added to R#
molten.R1 <- reshape2::melt(R1, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R2 <- reshape2::melt(R2, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R3 <- reshape2::melt(R3, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R4 <- reshape2::melt(R4, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R5 <- reshape2::melt(R5, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R6 <- reshape2::melt(R6, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R7 <- reshape2::melt(R7, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R8 <- reshape2::melt(R8, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R9 <- reshape2::melt(R9, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R10 <- reshape2::melt(R10, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R11 <- reshape2::melt(R11, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R12 <- reshape2::melt(R12, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R13 <- reshape2::melt(R13, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R14 <- reshape2::melt(R14, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R15 <- reshape2::melt(R15, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R16 <- reshape2::melt(R16, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R17 <- reshape2::melt(R17, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R18 <- reshape2::melt(R18, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R19 <- reshape2::melt(R19, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R20 <- reshape2::melt(R20, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R21 <- reshape2::melt(R21, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R22 <- reshape2::melt(R22, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R23 <- reshape2::melt(R23, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R24 <- reshape2::melt(R24, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R25 <- reshape2::melt(R25, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R26 <- reshape2::melt(R26, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R27 <- reshape2::melt(R27, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R28 <- reshape2::melt(R28, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R29 <- reshape2::melt(R29, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R30 <- reshape2::melt(R30, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R31 <- reshape2::melt(R31, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R32 <- reshape2::melt(R32, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R33 <- reshape2::melt(R33, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R34 <- reshape2::melt(R34, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R35 <- reshape2::melt(R35, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R36 <- reshape2::melt(R36, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R37 <- reshape2::melt(R37, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R38 <- reshape2::melt(R38, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R39 <- reshape2::melt(R39, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R40 <- reshape2::melt(R40, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R41 <- reshape2::melt(R41, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R42 <- reshape2::melt(R42, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R43 <- reshape2::melt(R43, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R44 <- reshape2::melt(R44, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R45 <- reshape2::melt(R45, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R46 <- reshape2::melt(R46, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R47 <- reshape2::melt(R47, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R48 <- reshape2::melt(R48, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R49 <- reshape2::melt(R49, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R50 <- reshape2::melt(R50, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R51 <- reshape2::melt(R51, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R52 <- reshape2::melt(R52, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R53 <- reshape2::melt(R53, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R54 <- reshape2::melt(R54, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R55 <- reshape2::melt(R55, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R56 <- reshape2::melt(R56, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R57 <- reshape2::melt(R57, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R58 <- reshape2::melt(R58, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R59 <- reshape2::melt(R59, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R60 <- reshape2::melt(R60, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R61 <- reshape2::melt(R61, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R62 <- reshape2::melt(R62, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R63 <- reshape2::melt(R63, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R64 <- reshape2::melt(R64, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R65 <- reshape2::melt(R65, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R66 <- reshape2::melt(R66, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R67 <- reshape2::melt(R67, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R68 <- reshape2::melt(R68, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R69 <- reshape2::melt(R69, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R70 <- reshape2::melt(R70, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R71 <- reshape2::melt(R71, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R72 <- reshape2::melt(R72, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R73 <- reshape2::melt(R73, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R74 <- reshape2::melt(R74, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R75 <- reshape2::melt(R75, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R76 <- reshape2::melt(R76, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R77 <- reshape2::melt(R77, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R78 <- reshape2::melt(R78, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R79 <- reshape2::melt(R79, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R80 <- reshape2::melt(R80, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R81 <- reshape2::melt(R81, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R82 <- reshape2::melt(R82, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R83 <- reshape2::melt(R83, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R84 <- reshape2::melt(R84, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R85 <- reshape2::melt(R85, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R86 <- reshape2::melt(R86, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R87 <- reshape2::melt(R87, id.vars="leg", variable.name="vote", value.name="pressure")
molten.R88 <- reshape2::melt(R88, id.vars="leg", variable.name="vote", value.name="pressure")

empty.records.roll.call <- attr (na.omit (molten.comp.party), "na.action")

# Below, all of the RC votes in bad.RC should be missing
good.RC <- molten.RC[-empty.records.roll.call,] 
bad.RC <- molten.RC[empty.records.roll.call,]  
bad.RC[!is.na(bad.RC$RC),] # should be empty

good.R1 <- molten.R1[-empty.records.roll.call,]
good.R2 <- molten.R2[-empty.records.roll.call,]
good.R3 <- molten.R3[-empty.records.roll.call,]
good.R4 <- molten.R4[-empty.records.roll.call,]
good.R5 <- molten.R5[-empty.records.roll.call,]
good.R6 <- molten.R6[-empty.records.roll.call,]
good.R7 <- molten.R7[-empty.records.roll.call,]
good.R8 <- molten.R8[-empty.records.roll.call,]
good.R9 <- molten.R9[-empty.records.roll.call,]
good.R10 <- molten.R10[-empty.records.roll.call,]
good.R11 <- molten.R11[-empty.records.roll.call,]
good.R12 <- molten.R12[-empty.records.roll.call,]
good.R13 <- molten.R13[-empty.records.roll.call,]
good.R14 <- molten.R14[-empty.records.roll.call,]
good.R15 <- molten.R15[-empty.records.roll.call,]
good.R16 <- molten.R16[-empty.records.roll.call,]
good.R17 <- molten.R17[-empty.records.roll.call,]
good.R18 <- molten.R18[-empty.records.roll.call,]
good.R19 <- molten.R19[-empty.records.roll.call,]
good.R20 <- molten.R20[-empty.records.roll.call,]
good.R21 <- molten.R21[-empty.records.roll.call,]
good.R22 <- molten.R22[-empty.records.roll.call,]
good.R23 <- molten.R23[-empty.records.roll.call,]
good.R24 <- molten.R24[-empty.records.roll.call,]
good.R25 <- molten.R25[-empty.records.roll.call,]
good.R26 <- molten.R26[-empty.records.roll.call,]
good.R27 <- molten.R27[-empty.records.roll.call,]
good.R28 <- molten.R28[-empty.records.roll.call,]
good.R29 <- molten.R29[-empty.records.roll.call,]
good.R30 <- molten.R30[-empty.records.roll.call,]
good.R31 <- molten.R31[-empty.records.roll.call,]
good.R32 <- molten.R32[-empty.records.roll.call,]
good.R33 <- molten.R33[-empty.records.roll.call,]
good.R34 <- molten.R34[-empty.records.roll.call,]
good.R35 <- molten.R35[-empty.records.roll.call,]
good.R36 <- molten.R36[-empty.records.roll.call,]
good.R37 <- molten.R37[-empty.records.roll.call,]
good.R38 <- molten.R38[-empty.records.roll.call,]
good.R39 <- molten.R39[-empty.records.roll.call,]
good.R40 <- molten.R40[-empty.records.roll.call,]
good.R41 <- molten.R41[-empty.records.roll.call,]
good.R42 <- molten.R42[-empty.records.roll.call,]
good.R43 <- molten.R43[-empty.records.roll.call,]
good.R44 <- molten.R44[-empty.records.roll.call,]
good.R45 <- molten.R45[-empty.records.roll.call,]
good.R46 <- molten.R46[-empty.records.roll.call,]
good.R47 <- molten.R47[-empty.records.roll.call,]
good.R48 <- molten.R48[-empty.records.roll.call,]
good.R49 <- molten.R49[-empty.records.roll.call,]
good.R50 <- molten.R50[-empty.records.roll.call,]
good.R51 <- molten.R51[-empty.records.roll.call,]
good.R52 <- molten.R52[-empty.records.roll.call,]
good.R53 <- molten.R53[-empty.records.roll.call,]
good.R54 <- molten.R54[-empty.records.roll.call,]
good.R55 <- molten.R55[-empty.records.roll.call,]
good.R56 <- molten.R56[-empty.records.roll.call,]
good.R57 <- molten.R57[-empty.records.roll.call,]
good.R58 <- molten.R58[-empty.records.roll.call,]
good.R59 <- molten.R59[-empty.records.roll.call,]
good.R60 <- molten.R60[-empty.records.roll.call,]
good.R61 <- molten.R61[-empty.records.roll.call,]
good.R62 <- molten.R62[-empty.records.roll.call,]
good.R63 <- molten.R63[-empty.records.roll.call,]
good.R64 <- molten.R64[-empty.records.roll.call,]
good.R65 <- molten.R65[-empty.records.roll.call,]
good.R66 <- molten.R66[-empty.records.roll.call,]
good.R67 <- molten.R67[-empty.records.roll.call,]
good.R68 <- molten.R68[-empty.records.roll.call,]
good.R69 <- molten.R69[-empty.records.roll.call,]
good.R70 <- molten.R70[-empty.records.roll.call,]
good.R71 <- molten.R71[-empty.records.roll.call,]
good.R72 <- molten.R72[-empty.records.roll.call,]
good.R73 <- molten.R73[-empty.records.roll.call,]
good.R74 <- molten.R74[-empty.records.roll.call,]
good.R75 <- molten.R75[-empty.records.roll.call,]
good.R76 <- molten.R76[-empty.records.roll.call,]
good.R77 <- molten.R77[-empty.records.roll.call,]
good.R78 <- molten.R78[-empty.records.roll.call,]
good.R79 <- molten.R79[-empty.records.roll.call,]
good.R80 <- molten.R80[-empty.records.roll.call,]
good.R81 <- molten.R81[-empty.records.roll.call,]
good.R82 <- molten.R82[-empty.records.roll.call,]
good.R83 <- molten.R83[-empty.records.roll.call,]
good.R84 <- molten.R84[-empty.records.roll.call,]
good.R85 <- molten.R85[-empty.records.roll.call,]
good.R86 <- molten.R86[-empty.records.roll.call,]
good.R87 <- molten.R87[-empty.records.roll.call,]
good.R88 <- molten.R88[-empty.records.roll.call,]

# If we want to admit "party changing" legislators, we will also need
# to get them "continuous" numbers
good.comp.party <- molten.comp.party[-empty.records.roll.call,]
# Legislator numbers are not continuous here
current.leg.no <- sort(unique (good.comp.party$new.leg.no))

# Now that we have separated legislators with missing party membership,
# and split party switchers into two (or more) parties, it is the case
# that some voting records will be completely missing (abstentions) 

legs.with.all.NAs <- ifelse (as.numeric(lapply (by (good.RC$RC, good.comp.party$new.leg.no, unique), length))==1, 1, 0)
legs.to.drop <- sort (unique (good.comp.party$new.leg.no))[legs.with.all.NAs==1]
empty.votes.roll.call <- c(1:nrow(good.comp.party))[is.element (good.comp.party$new.leg.no, legs.to.drop)]

# Now use empty.votes.roll.call to parse down both good.comp.party, and good.RC
good.comp.party <- good.comp.party[-empty.votes.roll.call,]
good.RC <- good.RC[-empty.votes.roll.call,]

good.R1 <- good.R1[-empty.votes.roll.call,]
good.R2 <- good.R2[-empty.votes.roll.call,]
good.R3 <- good.R3[-empty.votes.roll.call,]
good.R4 <- good.R4[-empty.votes.roll.call,]
good.R5 <- good.R5[-empty.votes.roll.call,]
good.R6 <- good.R6[-empty.votes.roll.call,]
good.R7 <- good.R7[-empty.votes.roll.call,]
good.R8 <- good.R8[-empty.votes.roll.call,]
good.R9 <- good.R9[-empty.votes.roll.call,]
good.R10 <- good.R10[-empty.votes.roll.call,]
good.R11 <- good.R11[-empty.votes.roll.call,]
good.R12 <- good.R12[-empty.votes.roll.call,]
good.R13 <- good.R13[-empty.votes.roll.call,]
good.R14 <- good.R14[-empty.votes.roll.call,]
good.R15 <- good.R15[-empty.votes.roll.call,]
good.R16 <- good.R16[-empty.votes.roll.call,]
good.R17 <- good.R17[-empty.votes.roll.call,]
good.R18 <- good.R18[-empty.votes.roll.call,]
good.R19 <- good.R19[-empty.votes.roll.call,]
good.R20 <- good.R20[-empty.votes.roll.call,]
good.R21 <- good.R21[-empty.votes.roll.call,]
good.R22 <- good.R22[-empty.votes.roll.call,]
good.R23 <- good.R23[-empty.votes.roll.call,]
good.R24 <- good.R24[-empty.votes.roll.call,]
good.R25 <- good.R25[-empty.votes.roll.call,]
good.R26 <- good.R26[-empty.votes.roll.call,]
good.R27 <- good.R27[-empty.votes.roll.call,]
good.R28 <- good.R28[-empty.votes.roll.call,]
good.R29 <- good.R29[-empty.votes.roll.call,]
good.R30 <- good.R30[-empty.votes.roll.call,]
good.R31 <- good.R31[-empty.votes.roll.call,]
good.R32 <- good.R32[-empty.votes.roll.call,]
good.R33 <- good.R33[-empty.votes.roll.call,]
good.R34 <- good.R34[-empty.votes.roll.call,]
good.R35 <- good.R35[-empty.votes.roll.call,]
good.R36 <- good.R36[-empty.votes.roll.call,]
good.R37 <- good.R37[-empty.votes.roll.call,]
good.R38 <- good.R38[-empty.votes.roll.call,]
good.R39 <- good.R39[-empty.votes.roll.call,]
good.R40 <- good.R40[-empty.votes.roll.call,]
good.R41 <- good.R41[-empty.votes.roll.call,]
good.R42 <- good.R42[-empty.votes.roll.call,]
good.R43 <- good.R43[-empty.votes.roll.call,]
good.R44 <- good.R44[-empty.votes.roll.call,]
good.R45 <- good.R45[-empty.votes.roll.call,]
good.R46 <- good.R46[-empty.votes.roll.call,]
good.R47 <- good.R47[-empty.votes.roll.call,]
good.R48 <- good.R48[-empty.votes.roll.call,]
good.R49 <- good.R49[-empty.votes.roll.call,]
good.R50 <- good.R50[-empty.votes.roll.call,]
good.R51 <- good.R51[-empty.votes.roll.call,]
good.R52 <- good.R52[-empty.votes.roll.call,]
good.R53 <- good.R53[-empty.votes.roll.call,]
good.R54 <- good.R54[-empty.votes.roll.call,]
good.R55 <- good.R55[-empty.votes.roll.call,]
good.R56 <- good.R56[-empty.votes.roll.call,]
good.R57 <- good.R57[-empty.votes.roll.call,]
good.R58 <- good.R58[-empty.votes.roll.call,]
good.R59 <- good.R59[-empty.votes.roll.call,]
good.R60 <- good.R60[-empty.votes.roll.call,]
good.R61 <- good.R61[-empty.votes.roll.call,]
good.R62 <- good.R62[-empty.votes.roll.call,]
good.R63 <- good.R63[-empty.votes.roll.call,]
good.R64 <- good.R64[-empty.votes.roll.call,]
good.R65 <- good.R65[-empty.votes.roll.call,]
good.R66 <- good.R66[-empty.votes.roll.call,]
good.R67 <- good.R67[-empty.votes.roll.call,]
good.R68 <- good.R68[-empty.votes.roll.call,]
good.R69 <- good.R69[-empty.votes.roll.call,]
good.R70 <- good.R70[-empty.votes.roll.call,]
good.R71 <- good.R71[-empty.votes.roll.call,]
good.R72 <- good.R72[-empty.votes.roll.call,]
good.R73 <- good.R73[-empty.votes.roll.call,]
good.R74 <- good.R74[-empty.votes.roll.call,]
good.R75 <- good.R75[-empty.votes.roll.call,]
good.R76 <- good.R76[-empty.votes.roll.call,]
good.R77 <- good.R77[-empty.votes.roll.call,]
good.R78 <- good.R78[-empty.votes.roll.call,]
good.R79 <- good.R79[-empty.votes.roll.call,]
good.R80 <- good.R80[-empty.votes.roll.call,]
good.R81 <- good.R81[-empty.votes.roll.call,]
good.R82 <- good.R82[-empty.votes.roll.call,]
good.R83 <- good.R83[-empty.votes.roll.call,]
good.R84 <- good.R84[-empty.votes.roll.call,]
good.R85 <- good.R85[-empty.votes.roll.call,]
good.R86 <- good.R86[-empty.votes.roll.call,]
good.R87 <- good.R87[-empty.votes.roll.call,]
good.R88 <- good.R88[-empty.votes.roll.call,]



# Rearrange good.comp.party by new.leg.no
good.comp.party <- good.comp.party[with(good.comp.party, order(good.comp.party$new.leg.no)),]
final.leg.no <- c()
for (i in 1:length (unique(good.comp.party$new.leg.no))){
  which.leg <- unique(good.comp.party$new.leg.no)[i]
  tmp <- rep (i, length (good.comp.party$new.leg.no[good.comp.party$new.leg.no==which.leg]))
  final.leg.no <- c (final.leg.no, tmp)
}
good.comp.party$final.leg.no <- final.leg.no
good.comp.party <- good.comp.party[with(good.comp.party, order(good.comp.party$original.order)),]

# Who are the legislators that vote very few times?
c(1:max(good.comp.party$final.leg.no))[as.numeric (by (good.RC$RC, good.comp.party$final.leg.no, length)) < 20]

# Other objects that need to be restructured
full.party <- good.comp.party$party
full.party.not.small.parties <- ifelse (full.party > 3, 4, full.party)
bill.no <- as.numeric(levels(good.RC$vote))[good.RC$vote]

# Expand matrix with high-standard leader positions to include a fourth row
forAgainstMatrix <- rbind (forAgainstMatrix, rep(0, ncol(forAgainstMatrix)))
positionMatrix <- rbind (positionMatrix, rep(0, ncol(positionMatrix)))
row.names(forAgainstMatrix)[4]  <- row.names(positionMatrix)[4] <-  "Other.Party.LeaderDecision"


position.vec <-  position.vec.strict  <- c()

for (i in 1:length(bill.no)){
  position.vec[i] <- positionMatrix[full.party.not.small.parties[i],bill.no[i]]
  position.vec.strict[i] <- forAgainstMatrix[full.party.not.small.parties[i],bill.no[i]]
}

potential.fix.bill <- c(22,120,144,150,166,270)

# List of objects to keep
objects_to_keep <- c("good.RC",
                     "good.comp.party",
                     "good.R7",
                     "good.R8",
                     "good.R9",
                     "good.R1",
                     "good.R2",
                     "good.R3",
                     "good.R67",
                     "good.R27",
                     "good.R26",
                     "good.R28",
                     "good.R64",
                     "good.R69",
                     "good.R68",
                     "good.R70",
                     "good.R65",
                     "good.R30",
                     "good.R29",
                     "good.R31",
                     "good.R75",
                     "good.R51",
                     "good.R50",
                     "good.R52",
                     "good.R72",
                     "good.R48",
                     "good.R47",
                     "good.R49",
                     "good.R77",
                     "good.R37",
                     "good.R33",
                     "good.R41",
                     "good.R71",
                     "good.R45",
                     "good.R44",
                     "good.R46",
                     "good.R73",
                     "good.R36",
                     "good.R32",
                     "good.R40",
                     "good.R56",
                     "position.vec.strict",
                     "potential.fix.bill")


path <- "mexico_rollcall_long_format_mean.RData"

# Uncomment next line to save all "mexico_rollcall_long_format_mean.RData"
# do.call("save", c(objects_to_keep, list(file = path)))
